<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : (dataForm.status=='2'?'反馈':'修改')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form v-loading="formLoading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <div v-if="dataForm.status=='0'">
        <el-form-item label="工单类型" prop="type">
            <el-radio-group v-model="dataForm.type">
                <el-radio :label="0">运维工单</el-radio>
                <el-radio :label="1">维修工单</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="设备" prop="equipmentId">
            <el-select
                style="width:100%"
                v-model="dataForm.equipmentId"
                filterable
                remote
                reserve-keyword
                placeholder="请输入关键字"
                :remote-method="remoteMethod"
                :loading="equipmentListLoading"
                v-loadmore="equipmentListLoadMore">
                <el-option
                v-for="item in equipmentList"
                :key="item.id"
                :label="item.name+' ('+item.rtuFarmlandName+')'"
                :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="负责人" prop="handler">
            <el-select
                style="width:100%"
                v-model="dataForm.handler"
                filterable
                remote
                reserve-keyword
                placeholder="请输入关键字"
                :remote-method="remoteMethod"
                :loading="handlerListLoading"
                v-loadmore="handlerListLoadMore">
                <el-option
                v-for="item in handlerList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                >
                </el-option>
            </el-select>
        </el-form-item>
       
        <el-form-item label="紧急度" prop="gradeNum">
            <!-- <el-input-number v-model="dataForm.gradeNum" controls-position="right" :min="0" placeholder="紧急度"></el-input-number> -->
            <el-select style="width:100%" v-model="dataForm.gradeNum" filterable placeholder="请选择紧急度">
                <el-option
                v-for="item in gjjbList"
                :key="item.id"
                :label="item.value"
                :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
         <el-form-item label="工单内容" prop="content">
            <el-input :rows="3" type="textarea" v-model="dataForm.content" placeholder="工单内容"></el-input>
        </el-form-item>
    </div>
    <div v-if="dataForm.status=='2'">
        <el-form-item label="结果状态" prop="resultStatus">
            <el-radio-group v-model="dataForm.resultStatus">
                <el-radio :label="0">一切正常</el-radio>
                <el-radio :label="1">恢复正常</el-radio>
                <el-radio :label="2">延后处理</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="处理结果" prop="resultContent">
            <el-input type="textarea" v-model="dataForm.resultContent" placeholder="处理结果"></el-input>
        </el-form-item>
        <el-form-item label="附件" prop="resultFileIds">
            <multipleFileUpload ref="multipleFileUploadRef"></multipleFileUpload>
        </el-form-item>
    </div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="dataForm.status==0" v-loading="formLoading" type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-button v-if="dataForm.status==2" v-loading="formLoading" type="primary" @click="dataFormSubmit2()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  import multipleFileUpload from '@/components/multiple-flie-upload/index'
  export default {
    components: {
      multipleFileUpload
    },
    data () {
      return {
        gjjbList: [],
        visible: false,
        formLoading: false,
        loading: false,
        dataForm: {
          id: 0,
          handler: '',
          equipmentId: '',
          resultStatus: 0,
          type: 0,
          content: '',
          gradeNum: '',
          status: 0,

          resultContent: '',
          resultFileIds: '',
          sourceType: '',
          dept: ''
        },
        equipmentListLoading: false,
        equipmentList: [],    // 处理人列表
        equipmentParams: {},    // 处理人查询参数
        handlerListLoading: false,
        handlerList: [],  // 处理人列表
        hadlerParams: {}, // 处理人查询参数
        deptList: [], //    部门
        dataRule: {
          handler: [
            { required: true, message: '负责人不能为空', trigger: 'blur' }
          ],
          equipmentId: [
            { required: true, message: '设备id不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '工单内容不能为空', trigger: 'blur' }
          ],
          gradeNum: [
            { required: true, message: '紧急度不能为空', trigger: 'blur' }
          ],
          sourceType: [
            { required: true, message: '来源类型 0：后台添加 1：告警添加不能为空', trigger: 'blur' }
          ],
        },
        options: []
      }
    },
    methods: {
      init (id) {
        this.equipmentListLoadMore("init")
        this.handlerListLoadMore("init")

        this.getTkList()

        this.dataForm.id = id || 0
        this.visible = true
        this.formLoading = false
        this.$nextTick(() => {
          this.dataForm.status = 0
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussmaintaintask/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.handler = data.bussMaintainTask.handler
                this.dataForm.equipmentId = data.bussMaintainTask.equipmentId
                this.dataForm.businessNum = data.bussMaintainTask.businessNum
                this.dataForm.type = data.bussMaintainTask.type
                this.dataForm.content = data.bussMaintainTask.content
                this.dataForm.gradeNum = data.bussMaintainTask.gradeNum
                this.dataForm.version = data.bussMaintainTask.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid && !this.formLoading) {
            this.formLoading = true
            this.$http({
              url: this.$http.adornUrl(`/buss/bussmaintaintask/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'handler': this.dataForm.handler,
                'equipmentId': this.dataForm.equipmentId,
                'type': this.dataForm.type,
                'status': this.dataForm.status,
                'content': this.dataForm.content,
                'gradeNum': this.dataForm.gradeNum,
                'version': this.dataForm.version,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.formLoading = false
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 接收工单
      receive(id){
        this.dataForm.id = id || 0
        this.$confirm(`确认接收该工单吗，接收后不可退回？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
              url: this.$http.adornUrl(`/buss/bussmaintaintask/update`),
              method: 'post',
              data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'status': 1,
              })
          }).then(({data}) => {
              if (data && data.code === 0) {
                  this.$message({
                      message: '操作成功',
                      type: 'success',
                      duration: 1500,
                      onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                      }
                  })
              } else {
                  this.$message.error(data.msg)
              }
          })
        })
      },
      finishInit(id){
          this.dataForm.id = id || 0

          this.visible = true
          this.formLoading = false
          this.dataForm.status = 2
          this.$nextTick(() => {
            // 清除上传缓存
            this.$refs['multipleFileUploadRef'].clearHistory()
            this.$refs['dataForm'].resetFields()
          })
      },
      // 反馈完成工单表单提交
      dataFormSubmit2 () {
        if(!this.formLoading){
          var uploadFileIds = this.$cookie.get('uploadFileIds')
          this.formLoading = true
          this.$http({
            url: this.$http.adornUrl(`/buss/bussmaintaintask/update`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'resultStatus': this.dataForm.resultStatus,
              'status': this.dataForm.status,
              'resultContent': this.dataForm.resultContent,
              'resultFileIds': this.$cookie.get('uploadFileIds'),
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
              // 请求完成后 删除上传的id
              this.$refs['multipleFileUploadRef'].clearHistory()
            } else {
              this.formLoading = false
              this.$message.error(data.msg)
            }
          })
        }
      },
      // 这里下拉框查询触发的方法
      remoteMethod(query) {
        this.equipmentListLoadMore('query',query)
      },
      // 这里写入滚动加载要触发的方法
      equipmentListLoadMore (type,query) {
        this.equipmentListLoading = true

        if (type && (type == 'init' || type == 'query')){
            this.equipmentParams.page = 1
            this.equipmentList = []
        } else if (this.equipmentParams.page) {
            if (this.equipmentParams.page >= this.equipmentParams.totalPage){
                this.equipmentListLoading = false
                return
            }
            this.equipmentParams.page += 1
        }

        var _this = this
        this.$http({
            url: this.$http.adornUrl('/buss/bussrtuequipment/list'),
            method: 'get',
            params: this.$http.adornParams({
                "name": query, //搜索参数
                "page": _this.equipmentParams.page || 1, //分页参数
                "limit": _this.equipmentParams.limit || 15, //每次查询15条记录
            })
        }).then(({data}) => {
            _this.equipmentListLoading = false
            _this.equipmentParams.totalPage = data.page.totalPage
            _this.equipmentList = _this.equipmentList.concat(data.page.list)
        })
      },
      // 这里下拉框查询触发的方法
      remoteMethod (query) {
        this.handlerListLoadMore('query',query)
      },
      // 这里写入滚动加载要触发的方法
      handlerListLoadMore (type,query) {
        this.handlerListLoading = true

        if(type && (type == 'init' || type == 'query')){
            this.hadlerParams.page = 1
            this.handlerList = []
        }else if(this.hadlerParams.page){
            if(this.hadlerParams.page>=this.hadlerParams.totalPage){
                this.handlerListLoading = false
                return
            }
            this.hadlerParams.page += 1
        }

        var _this = this
        this.$http({
            url: this.$http.adornUrl('/sys/user/list2'),
            method: 'get',
            params: this.$http.adornParams({
                "name": query, //搜索参数
                "page": _this.hadlerParams.page || 1, //分页参数
                "limit": _this.hadlerParams.limit || 15, //每次查询15条记录
            })
        }).then(({data}) => {
            _this.handlerListLoading = false
            _this.hadlerParams.totalPage = data.page.totalPage
            _this.handlerList = _this.handlerList.concat(data.page.list)
        })
      },
      getTkList () {
        // 获取告警级别字典列表
        this.$http({
          url: this.$http.adornUrl('/sys/dict/select'),
          method: 'get',
          params: this.$http.adornParams({
              "type": "alarm_level_00"
          })
        }).then(({data}) => {
          this.gjjbList = treeDataTranslate(data.list, 'id')
        })   	     	       	
      },
    }
  }
</script>
